#!/bin/bash 
# Copyright 2007 David Stone
# Distributed under the terms of the GNU General Public License v2
# http://builtbydave.co.uk

# Path to Policy file
XPAYPOLICY=/usr/local/xpay/policy

# Set the location of the XPay jar file
CLASSPATH=/usr/local/xpay/XPay.jar

# Path to java executable
JAVAPATH=/etc/alternatives/jre_1.4.2/bin/java

# Source function library.
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
DAEMON="$JAVAPATH -cp $CLASSPATH -Djava.security.manager -Djava.security.policy=$XPAYPOLICY XPay"
PIDFILE=/var/run/xpay.pid
LOGFILE=/var/log/xpay

sanity_checks() {
	# check pid doesn't exist.
	if [ -f $PIDFILE ]; then
		/bin/echo "ERROR: PID file $PIDFILE already exists."
		exit 1
	fi 
}

#
# main()
#

case "${1:-''}" in
	'start')
		sanity_checks
		$DAEMON &> $LOGFILE & echo $! > $PIDFILE
		if [ -f $PIDFILE ]; then
			action "Starting Xpay service..." /bin/true
			exit 1
		fi
		action "Starting Xpay service..." /bin/false
		;;
	'stop')
		if [ -f $PIDFILE ]; then
			kill `cat $PIDFILE`
			rm $PIDFILE
			if [ -f $PIDFILE ]; then
				action "Stopping Xpay service..." /bin/false
				exit 1
			fi
			action "Stopping Xpay service..." /bin/true
			exit 1
		fi
		action "Stopping Xpay service..." /bin/false
		exit 1
		;;
	'restart')
		$SELF stop
		$SELF start 
		;;
  	*)
		/bin/echo "Usage: $SELF start|stop|restart"
		exit 1
		;;
esac

